using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using THSLib;

namespace THSERP.BaseProvider
{
    public partial class guiContactType : THSERPScreen
    {
        #region constant for ADConfigValue
        public const string ADConfigValueContactType = "ContactType";

        #endregion        

        protected String _contactTypeControl;        
        public guiContactType()
        {
            InitializeComponent();
        }

        public guiContactType(String strContactTypeControl)
        {
            InitializeComponent();
            ContactTypeControl = strContactTypeControl;            
        }

        protected String ContactTypeControl
        {
            get
            {
                return _contactTypeControl;
            }
            set
            {
                _contactTypeControl = value;
            }
        }        

        #region Init Control Functions

        // changes, 27/03/2008, init control of contact types screen
        private void InitControl()
        {

            //String strMainObjectInfo = ((BaseModuleERP)Module).CurrentModuleEntity.MainObject.ToString().Substring(8);
            //String strMainObjectName = strMainObjectInfo.Substring(0, strMainObjectInfo.Length - 4);
            //String strContactsObjectName = strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "Contacts";

            // change, 16.04.2008
            
            String strMainObjectName = THSUtil.GetTableNameFromBusinessObject(((BaseModuleERP)Module).CurrentModuleEntity.MainObject);
            String strMainObjectInfo = strMainObjectName + "Info";
            String strContactsObjectName = strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "Contacts";
            String strMainObjectPrefix = strMainObjectName.Substring(0, 2);

            BusinessObject objContactsInfo = ((BaseModuleERP)Module).CurrentModuleEntity.ModuleObjects[strContactsObjectName];
            //PropertyInfo[] properties= BusinessObjectFactory.GetBusinessObjectType(strContactsObjectName + "Info").GetProperties();

            String strContactType = String.Empty;

                        

            //if (objContactsInfo != null)
            //{
            //    foreach (PropertyInfo property in properties)
            //        if (property.Name.Contains("ContactType"))
            //            if (!property.Name.Contains("ContactTypeDisplay"))
            //                strContactType = property.GetValue(objContactsInfo, null).ToString();
            //}

            // change, 16.04.2008
            String strContactTypeColumnName = strMainObjectPrefix + "ContactType";
            if (THSDbUtil.ColumnIsExist(strContactsObjectName, strContactTypeColumnName))
            {
                strContactType=THSDbUtil.GetPropertyValue(objContactsInfo, strContactTypeColumnName).ToString();
            }

            InitControlText();

            String[] arrContactTypes = strContactType.Split(new char[] { '_' });
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "Proposal") >= 0)
                fld_chkContactTypeProposal.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "Proposal");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "SaleOrder") >= 0)
                fld_chkContactTypeSellOrder.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "SaleOrder");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "Invoice") >= 0)
                fld_chkContactTypeInvoice.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "Invoice");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "Delivery") >= 0)
                fld_chkContactTypeDelivery.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "Delivery");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "SaleReturn") >= 0)
                fld_chkContactTypeSaleReturn.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "SaleReturn");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "CreditNote") >= 0)
                fld_chkContactTypeCreditNote.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "CreditNote");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "Receipt") >= 0)
                fld_chkContactTypeReceipt.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "Receipt");

            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "ProposalCopy") >= 0)
                fld_chkContactTypeProposalReceipt.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "ProposalCopy");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "SaleOrderCopy") >= 0)
                fld_chkContactTypeSellOrderReceipt.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "SaleOrderCopy");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "InvoiceCopy") >= 0)
                fld_chkContactTypeInvoiceReceipt.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "InvoiceCopy");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "DeliveryCopy") >= 0)
                fld_chkContactTypeDeliveryReceipt.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "DeliveryCopy");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "SaleReturnCopy") >= 0)
                fld_chkContactTypeSaleReturnReceipt.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "SaleReturnCopy");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "CreditNoteCopy") >= 0)
                fld_chkContactTypeCreditNoteReceipt.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "CreditNoteCopy");
            if (Array.IndexOf(arrContactTypes, ADConfigValueUtility.ADConfigValueContactType + "ReceiptCopy") >= 0)
                fld_chkContactTypeReceiptReceipt.Checked = strContactType.Contains(ADConfigValueUtility.ADConfigValueContactType + "ReceiptCopy");
        }

        private void InitControlText()
        {
            fld_chkContactTypeInvoice.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "Invoice");
            fld_chkContactTypeProposal.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "Proposal");
            fld_chkContactTypeSellOrder.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "SaleOrder");
            fld_chkContactTypeDelivery.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "Delivery");
            fld_chkContactTypeSaleReturn.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "SaleReturn");
            fld_chkContactTypeCreditNote.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "CreditNote");
            fld_chkContactTypeReceipt.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "Receipt");

            fld_chkContactTypeInvoiceReceipt.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "InvoiceCopy");
            fld_chkContactTypeProposalReceipt.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "ProposalCopy");
            fld_chkContactTypeSellOrderReceipt.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "SaleOrderCopy");
            fld_chkContactTypeDeliveryReceipt.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "DeliveryCopy");
            fld_chkContactTypeSaleReturnReceipt.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "SaleReturnCopy");
            fld_chkContactTypeCreditNoteReceipt.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "CreditNoteCopy");
            fld_chkContactTypeReceiptReceipt.Text = ADConfigValueUtility.GetTextFromKey(ADConfigValueUtility.ADConfigValueContactType + "ReceiptCopy");
        }

 
        #endregion

        #region Set Contact Type Functions
  
        // changes, 27/03/2008, update contact types user chooses
        private void UpdateContactType()
        {
            ERPModuleEntities entity = (Module as BaseModuleERP).CurrentModuleEntity;
            String strContactType = String.Empty;
            String strContactTypeDisplay = String.Empty;
            
            if (fld_chkContactTypeProposal.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "Proposal") + '_';
                strContactTypeDisplay += (fld_chkContactTypeProposal.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeSellOrder.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "SaleOrder") + '_';
                strContactTypeDisplay += (fld_chkContactTypeSellOrder.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeDelivery.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "Delivery") + '_';
                strContactTypeDisplay += (fld_chkContactTypeDelivery.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeInvoice.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "Invoice") + '_';
                strContactTypeDisplay += (fld_chkContactTypeInvoice.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeSaleReturn.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "SaleReturn") + '_';
                strContactTypeDisplay += (fld_chkContactTypeSaleReturn.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeCreditNote.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "CreditNote") + '_';
                strContactTypeDisplay += (fld_chkContactTypeCreditNote.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeReceipt.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "Receipt") + '_';
                strContactTypeDisplay += (fld_chkContactTypeReceipt.Text.Substring(0, 3) + "-");
            }


            
            if (fld_chkContactTypeProposalReceipt.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "ProposalCopy") + '_';
                strContactTypeDisplay += (fld_chkContactTypeProposalReceipt.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeSellOrderReceipt.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "SaleOrderCopy") + '_';
                strContactTypeDisplay += (fld_chkContactTypeSellOrderReceipt.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeDeliveryReceipt.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "DeliveryCopy") + '_';
                strContactTypeDisplay += (fld_chkContactTypeDeliveryReceipt.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeInvoiceReceipt.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "InvoiceCopy") + '_';
                strContactTypeDisplay += (fld_chkContactTypeInvoiceReceipt.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeSaleReturnReceipt.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "SaleReturnCopy") + '_';
                strContactTypeDisplay += (fld_chkContactTypeSaleReturnReceipt.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeCreditNoteReceipt.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "CreditNoteCopy") + '_';
                strContactTypeDisplay += (fld_chkContactTypeCreditNoteReceipt.Text.Substring(0, 3) + "-");
            }
            if (fld_chkContactTypeReceiptReceipt.Checked)
            {
                strContactType += (ADConfigValueUtility.ADConfigValueContactType + "ReceiptCopy") + '_';
                strContactTypeDisplay += (fld_chkContactTypeReceiptReceipt.Text.Substring(0, 3) + "-");
            }

            //String strMainObjectInfo = ((BaseModuleERP)Module).CurrentModuleEntity.MainObject.ToString().Substring(8);
            //String strMainObjectName = strMainObjectInfo.Substring(0, strMainObjectInfo.Length - 4);
            //String strContactsObjectName = strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "Contacts";

            // change, 16.04.2008
            String strMainObjectName = THSUtil.GetTableNameFromBusinessObject(((BaseModuleERP)Module).CurrentModuleEntity.MainObject);
            String strMainObjectInfo = strMainObjectName + "Info";
            String strContactsObjectName = strMainObjectName.Substring(0, strMainObjectName.Length - 1) + "Contacts";
            String strMainObjectPrefix = strMainObjectName.Substring(0, 2);

            BusinessObject objContactsInfo = ((BaseModuleERP)Module).CurrentModuleEntity.ModuleObjects[strContactsObjectName];
            //PropertyInfo[] properties = BusinessObjectFactory.GetBusinessObjectType(strContactsObjectName + "Info").GetProperties();

            if (objContactsInfo != null)
            {
                //foreach (PropertyInfo property in properties)
                //    if (property.Name.Contains("ContactType"))
                //    {
                //        if (property.Name.Contains("ContactTypeDisplay"))
                //            property.SetValue(objContactsInfo, strContactTypeDisplay, null);
                //        else
                //            property.SetValue(objContactsInfo, strContactType, null);
                //    }

                // change,16.04.2008
                String strContactTypeColumnName = strMainObjectPrefix + "ContactType";
                String strContactTypeDisplayColumnName = strContactsObjectName.Substring(0,strContactsObjectName.Length-1) + "TypeDisplay";

                
                if (THSDbUtil.ColumnIsExist(strContactsObjectName, strContactTypeColumnName))
                {
                    THSDbUtil.SetPropertyValue(objContactsInfo, strContactTypeColumnName,strContactType);
                }
                if (THSDbUtil.ColumnIsExist(strContactsObjectName, strContactTypeDisplayColumnName))
                {
                    THSDbUtil.SetPropertyValue(objContactsInfo, strContactTypeDisplayColumnName, strContactTypeDisplay);
                }

            }
            entity.UpdateModuleObjectBindingSource(strContactsObjectName);
        }
        #endregion

        private void guiContactType_Load(object sender, EventArgs e)
        {
            InitControl();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            UpdateContactType();
            this.Close();
        }
    }
}